/*
 * Copyright (c) 2022-2023 Termony Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HDF_UART_H
#define HDF_UART_H

#include "hdf_device_desc.h"
#include "osal_mutex.h"
#include "uart_core.h"
#include "hdf_dlist.h"
#include "hdf_sbuf.h"

#include "rk_parameters.h"

#ifdef __cplusplus
extern "C" {
#endif

#define UART_MAX_NUM UART_NUM

#define UART_MODE_POLL_RX 0x01 /* 轮询(阻塞)方式接收. */
#define UART_MODE_ISR_RX  0x02 /* 中断(非阻塞)方式接收. */
#define UART_MODE_DMA_RX  0x04 /* DMA(非阻塞)方式接收. */
#define UART_MODE_MASK_RX 0x0f /* UART接收模式掩码. */

#define UART_MODE_POLL_TX 0x10 /* 轮询(阻塞)方式发送. */
#define UART_MODE_ISR_TX  0x20 /* 中断(非阻塞)方式发送. */
#define UART_MODE_DMA_TX  0x40 /* DMA(非阻塞)方式发送. */
#define UART_MODE_TX_MASK 0xf0 /* UART发送模式掩码. */

#define UART_HWI_EVENT_SEND_REQUEST  0x0001UL /* 串口发送请求 */
#define UART_HWI_EVENT_SEND_CMPL     0x0002UL /* 串口发送完成 */
#define UART_HWI_EVENT_RECV_CMPL     0x0004UL /* 串口接收完成 */
#define UART_HWI_EVENT_RECV_OVERFLOW 0x0008UL /* 接收缓冲溢出 */
#define UART_HWI_EVENT_ERROR_OVERRUN 0x0010UL /* 串口溢出错误 */
#define UART_HWI_EVENT_ERROR_BREAK   0x0020UL /* 串口突发错误 */
#define UART_HWI_EVENT_ERROR_PRITY   0x0040UL /* 串口校验错误 */
#define UART_HWI_EVENT_ERROR_FRAMING 0x0080UL /* 串口帧错误 */
#define UART_HWI_EVENT_MODEM_RI      0x0100UL /* 响铃中断 */
#define UART_HWI_EVENT_MODEM_CTS     0x0200UL /* 清除发送中断 */
#define UART_HWI_EVENT_MODEM_DCD     0x0400UL /* 数据载波检测 */
#define UART_HWI_EVENT_MODEM_DSR     0x0800UL /* 数据就绪中断 */

struct UartDataItem {
    struct DListHead list;
    uint32_t size;
    uint32_t used;
};

struct UartCb {
    struct UartHost *host;
    // Uart instance;
    bool initFlag;

    struct HdfSBuf *data;
    struct DListHead recv;
    uint32_t usedRecvLen;
    struct OsalMutex rxMux;
    struct DListHead send;
    uint32_t usedSendLen;
    struct OsalMutex txMux;

    struct {
        const char *name;
        uint8_t uartIfId;
        uint8_t isMio;
        uint8_t transMode;
        uint8_t irqPriority;
        uint8_t loopback;
        uint8_t rtsEnable;
        uint8_t ctsEnable;
        uint16_t rxBufSize;
        uint16_t txBufSize;
        // UartFormat fmt;
    } cfg;
};

int32_t HDF_UartIsrRegister(struct UartCb *uartCb);
int32_t HDF_UartIsrUnregister(struct UartCb *uartCb);

int32_t HDF_UartIsrRxInit(struct UartCb *uartCb);
int32_t HDF_UartIsrRxDeInit(struct UartCb *uartCb);

int32_t HDF_UartIsrTxInit(struct UartCb *uartCb);
int32_t HDF_UartIsrTxDeInit(struct UartCb *uartCb);

int32_t HDF_UartModemInit(struct UartCb *uartCb);
int32_t HDF_UartModemDeInit(struct UartCb *uartCb);

int32_t HDF_UartIsrSend(struct UartCb *uartCb);

int32_t HDF_UartInit(struct UartCb *uartCb);
int32_t HDF_UartDeInit(struct UartCb *uartCb);

int32_t HDF_UartServiceRequest(struct UartCb *uartCb, uint32_t event, uint32_t size);

int32_t HDF_ConvertBaudRate(struct UartCb *uartCb, uint32_t baudRate, bool fixed);
int32_t HDF_ConvertDataBits(struct UartCb *uartCb, uint32_t dataBits, bool fixed);
int32_t HDF_ConvertParity(struct UartCb *uartCb, uint32_t parity, bool fixed);
int32_t HDF_ConvertStopBits(struct UartCb *uartCb, uint32_t stopBits, bool fixed);
int32_t HDF_ConvertTransMode(struct UartCb *uartCb, uint32_t transMode, bool fixed);

int32_t HDF_GetUartDesc(const char *name, uint8_t *uartIfId);
int32_t HDF_UartHostParser(struct UartHost *host, struct HdfDeviceObject *device);

#ifdef __cplusplus
}
#endif

#endif /* HDF_UART_H */